<template>
  <div class="planDetaill">
    <el-form :model="queryParams" ref="queryForm" :inline="true" style="margin-top: 20px" label-width="90px">
      <el-form-item label="计划月份" >
        <el-date-picker
        class="datawidth"
          v-model="queryParams.planMonth"
          type="month"
          size="small"
          format="yyyy-MM"
          value-format="yyyy-MM"
          @change="changeMonth"
          placeholder="选择月">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="申请日期">
        <el-date-picker v-model="date" size="small" value-format="yyyy-MM-dd" class="datawidth"
                        type="daterange" range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期"
                        @change="dateSelect"></el-date-picker>
      </el-form-item>
      <el-form-item label="计划类型" >
        <el-select
          v-model="queryParams.planType"
          placeholder="请选择"
          clearable
          size="small"
        >
          <el-option
            v-for="dict in statusOptions"
            :key="dict.dictValue"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="战线区队" >
          <el-select size="small" v-model="queryParams.wnameSJ" placeholder="请选择区队战线" ref="selectReport">
            <el-option :value="queryParams.depId" :label="queryParams.deptName" style="width:199px;height:200px;overflow: auto;background-color:#fff">
              <el-tree
                highlight-current
                 accordion
                :data="reportData"
                :props="defaultProps"
                @node-click="handleNodeClickSJ"
              ></el-tree>
            </el-option>
          </el-select>
      </el-form-item>
      <el-form-item label="审核状态" >
        <el-select
          v-model="queryParams.auditSteps"
          placeholder="请选择"
          clearable
          size="small"
        >
          <el-option
            v-for="dict in shbzList"
            :key="dict.dictValue"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="物料名称" >
        <el-input
          v-model="queryParams.MName"
          placeholder="请输入物料名称"
          clearable
          size="small"
        />
      </el-form-item>
      <el-form-item label="物料编码" >
        <el-input
          v-model="queryParams.mNo"
          placeholder="请输入物料编码"
          clearable
          size="small"
        />
      </el-form-item>
       <el-form-item label="费用项目" >
        <el-select
          v-model="queryParams.cpName"
          placeholder="请选择费用项目"
          clearable
          size="small"
        >
          <el-option
            v-for="(dict,index) in fee"
            :key="index"
            :label="dict.cpName"
            :value="dict.cpName"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="search" style="margin-left: 10px">查询</el-button>
        <el-button type="warning" icon="el-icon-download" size="mini" @click="exportList" >导出</el-button>
      </el-form-item>
    </el-form>
    <el-table v-loading="loading" :data="roleList" stripe border>
      <el-table-column align="center" type="index" label="序号"  width="55"  />
      <el-table-column align="center" label="部门名称"  property="depName" ></el-table-column>
      <el-table-column align="center" label="审核状态"   property="auditSteps" :show-overflow-tooltip="true"  >
        <template slot-scope="scope">
          <div style="background:#9E71DC  ;color: white;padding-left:10px;padding-right:10px;display: inline-block" v-show="scope.row.auditSteps=='0'">未提交</div>
          <div style="background:#78BF34  ;color: white;padding-left:10px;padding-right:10px;display: inline-block" v-show="scope.row.auditSteps=='1'">战线审核</div>
          <div style="background:#9E71DC  ;color: white;padding-left:10px;padding-right:10px;display: inline-block" v-show="scope.row.auditSteps=='2'">经营科审核</div>
          <div style="background:#9E71DC  ;color: white;padding-left:10px;padding-right:10px;display: inline-block" v-show="scope.row.auditSteps=='3'">利库审核</div>
          <div style="background:#9E71DC  ;color: white;padding-left:10px;padding-right:10px;display: inline-block" v-show="scope.row.auditSteps=='4'">经营科长审核</div>
          <div style="background:#9E71DC  ;color: white;padding-left:10px;padding-right:10px;display: inline-block" v-show="scope.row.auditSteps=='5'">驳回</div>
          <div style="background:#9E71DC  ;color: white;padding-left:10px;padding-right:10px;display: inline-block" v-show="scope.row.auditSteps=='6'">通过</div>
          <div style="background:#9E71DC  ;color: white;padding-left:10px;padding-right:10px;display: inline-block" v-show="scope.row.auditSteps=='7'">废单</div>
        </template>

      </el-table-column>

      <el-table-column align="center" label="编码"  property="mno"  width="155"></el-table-column>
      <el-table-column align="center" label="物料名称" property="mname" :show-overflow-tooltip="true"  ></el-table-column>
      <el-table-column align="center" label="规格"  property="mmodel" ></el-table-column>
      <el-table-column align="center" label="单位"  property="unit" ></el-table-column>
      <el-table-column align="center" label="计划价"  property="planPrice" ></el-table-column>
      <el-table-column align="center" label="需求量" property="demandNum" :show-overflow-tooltip="true" ></el-table-column>
      <el-table-column align="center" label="金额/元"  property="totalAmount" >
        <template slot-scope="scope">
         <span>{{(scope.row.demandNum/1)*(scope.row.planPrice/1)}}</span>
        </template>
      </el-table-column>
       <el-table-column align="center" label="创建时间"  property="crtTime" ></el-table-column>
      <el-table-column align="center" label="利库数量"  property="loseNum" ></el-table-column>
       <el-table-column align="center" label="费用项目"  property="cpName" ></el-table-column>
      <el-table-column align="center" label="计划类型"  property="planType" >
        <template slot-scope="scope">
          <div style="background:#78BF34  ;color: white;padding-left:10px;padding-right:10px;display: inline-block" v-show="scope.row.planType=='1'">月度计划</div>
          <div style="background:#9E71DC  ;color: white;padding-left:10px;padding-right:10px;display: inline-block" v-show="scope.row.planType=='2'">临时计划</div>
        </template>
      </el-table-column>
    </el-table>
    <div class="bottomflot">
      <pagination
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="search"
      >
      </pagination>
      <span class="box">当页总金额: <b>{{pagePrice}}</b>   总金额: <b>{{price}}</b> </span>
    </div>
  </div>
</template>

<script>
  import request from '@/utils/request'

  export default {
    name: "Role",
    components:{
      // xqjhDialog,
    },
    data() {
      return {
        //当页总金额
        pagePri:0,
        //总金额
        price:0,
        shbzList:[
          { value: '0',label: '未提交'},
          { value: '1',label: '战线审核'},
          { value: '2',label: '经营科审核'},
          { value: '3',label: '利库审核'},
          { value: '4',label: '经营科长审核'},
          { value: '5',label: '驳回'},
          { value: '6',label: '通过'},
          { value: '7',label: '废单'},
        ],
        jhbzList:[
          { value: '1',label: '月度计划'},
          { value: '2',label: '临时计划'},
        ],
        // 状态数据字典
        statusOptions: [
          { value: '1',label: '月度计划'},
          { value: '2',label: '临时计划'},
        ],


        xqjhShow:false,
        xqjhStr:{},
        // 遮罩层
        loading: false,
        // 总条数
        total: 0,
        // 角色表格数据
        roleList: [],
        // 日期范围
        dateRange: [],
        //资金来源
        zjlyList:[
          { value: '1',label: '来源1'},
          { value: '2',label: '来源2'},
        ],
        //计划状态
        jhztList:[
          { value: '1',label: '草稿箱'},
          { value: '2',label: '已提交'},
        ],
        // 菜单列表
        menuOptions: [],
        // 部门列表
        deptOptions: [],
        //战线区队
        reportData:[],
        //费用列表
        fee:[],
        //日期范围
        date:[],
        //日期展示
        // showDate:true,
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          planMonth:'',
          planType:'',
          MName:'',
          auditSteps:'',
          cpName:'',
          depId:'0',
          wnameSJ:'全部',
          applyBeginTime:'',
          applyEndTime:'',
          mNo:''
        },
        // 表单参数
        form: {},
        defaultProps: {
          children: "children",
          label: "deptName"
        },
        //导出参数
        // dcME:{
        //   planType:'',
        //   planMonth:'',
        //   MName:'',
        //   auditSteps:'',
        //   moneyName:''
        // }
      };
    },
    computed: {
      pagePrice () {
        this.pagePri=0,
        this.roleList.forEach(item => {
          this.pagePri+=(item.demandNum)*(item.planPrice)
          console.log(this.pagePri)
        });
        this.pagePri = this.pagePri.toFixed(2)
        console.log(this.pagePri)
        this.pagePri = this.pagePri.toString()
        console.log(this.pagePri)
        this.pagePri = this.pagePri.toLocaleString()
        return this.pagePri
      }
    },
    created() {
      var myDate = new Date();
      var yue = myDate.getMonth()+1
      if(yue<9){
        yue = '0'+yue
      }
      this.queryParams.planMonth =  myDate.getFullYear()+'-'+ yue+'';
      this.getZhanxin()
      this.search()
      this.getFee()
    },
    methods: {
      //获取站线数据
      getZhanxin:function(){
        request({
          url: 'system/dept/treeselectNoTeam',
          method: 'get',
        }).then(response => {
          this.reportData = response.data
          this.reportData.unshift(
            {deptId:'0',deptName:'全部',}
          )
        });
      },
       handleNodeClickSJ:function(node){
        console.log(node,'node')
        this.queryParams.depId = node.deptId
        this.queryParams.wnameSJ = node.deptName
        this.$refs.selectReport.blur()
        console.log(this.queryParams)
      },
      //获取费用项目
      getFee () {
        request({
          url:"/method/costproject/listCpName",
          method:'get'
        }).then(response=>{
          console.log(response)
          this.fee = response.data
        })
      },
      //日期范围
      dateSelect() {
        console.log(this.date)
        if(this.date) {
          this.queryParams.applyBeginTime = this.date[0]
          this.queryParams.applyEndTime = this.date[1]
        }else if(this.date==''||this.date==null) {
          // this.showDate = true
          this.queryParams.applyEndTime='',
          this.queryParams.applyBeginTime=''
        }
      },
      //日期更改
      changeMonth (month) {
        console.log(this.queryParams.planMonth)
        // console.log(month)
        // this.queryParams.planMonth = month
        // if(this.queryParams.planMonth==''||this.queryParams.planMonth==null) {
        //   this.showDate=false
        // }
        // if(this.date.length>0) {
        //   this.date=''
        //   this.queryParams.applyBeginTime='',
        //   this.queryParams.applyEndTime='',
        //   this.showDate = true
        // }
      },
      //监听
      jianting:function(row){
        console.log('row',row)

      },
      //新增回调函数
      xujhBack:function(row){
        console.log('返回数据',row)
        this.xqjhShow = false
      },
      //复选框监听
      handleSelectionChange:function(val){
        console.log(val)
      },
      //更多操作按钮
      handleCommand:function(command){
        // this.$message('click on item ' + command);
        if(command=='1'){
          var pp={
            title:'新增需求计划',
          }
          this.xqjhStr = pp
          this.xqjhShow = true
        }
      },
      //总金额
      allPrice () {
          console.log(this.queryParams)
         request({
            url:'system/info/selecTotalMoney',
            method:'get',
            params:this.queryParams
         }).then(response =>{
            console.log(response.data)
            this.price =  response.data
            // this.price = parseFloat(this.price).toLocaleString()
            return this.price
         })
      },
      //查询
      search:function(){
        this.loading = true
        // this.dcME.auditSteps = this.queryParams.auditSteps
        request({
          url: '/system/info/listByDetail',
          method: 'get',
          params:this.queryParams
        }).then(response => {
          this.loading = false
          this.roleList = response.rows
          // this.pagePri()
          this.allPrice()
          console.log(this.roleList)
          this.total = response.total
        });
      },
      /** 导出按钮操作 */
      exportList() {
        this.$confirm('是否确认导出?', '警告', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(()=>{
          this.derive()
          console.log(456)
          // return this.download(res.data)
        }).catch((e)=> {
        })
      },
      //导出
      derive() {
        // console.log(this.dcME)
        request({
          url:'system/demandPlan/exports',
          method:'get',
          params:this.queryParams
        }).then(response =>{
          console.log(response)
           return this.download(response.data)
        })
      },
      //驳回
      handleEdit:function (index,row) {
        this.$confirm('确认驳回?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.saveBtn(row,5)
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消驳回'
          });
        });
      },
      saveBtn:function(row,num){
        var obj={}
        obj.id = row.id
        obj.auditSteps = num
        request({
          url: '/system/demandPlan/steps',
          method: 'put',
          data: obj
        }).then(response => {
          if(response.code=='200'){
            this.$message({
              type: 'success',
              message: '操作成功!'
            });
          }
          this.search();
        });
      },
      //通过
      handleDelete:function (index,row) {
        this.$confirm('确认通过?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.saveBtn(row,5)
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消通过'
          });
        });
      },
      //详情点击
      xingqing:function (row) {
        console.log(row)
        this.$router.push({path:'/material/kclzspXQ',query:{data:row}});
      },

    }
  };
</script>
<style lang="scss">
  .planDetaill {
    .bottomflot {
      height: 80px;
    }
    .pagination-container {
      display: inline-block;
      position: relative;
      height: 25px;
      margin-bottom: 10px;
      margin-top: 15px;
      padding: 10px 20px !important;
      float: right;
    }
    .box{
      float: left;
      font-size: 13px;
      color: #606266;
      line-height: 80px;
      margin-left: 15px;
    }
  }

 .datawidth {
    width: 250px;
  }
.el-input--small .el-input__inner {
  width: 250px;
}
.el-date-editor.el-input, .el-date-editor.el-input__inner{
   width: 250px;
}
</style>
